#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/4/12  18:11
# @Author: 余浪人
# @email: yulangren520@gmail.com
from wtforms import Form, StringField, validators, SelectField, TextAreaField, DateField, BooleanField
from apps.models.column_model import Column_model
from apps.lib.public import random_click


class Document_Form(Form):
    def __init__(self, *args, **kwargs):
        super(Document_Form, self).__init__(*args, **kwargs)
        if not Column_model.query.count():
            self.superior.choices = [(0, '请先添加分类')]
        else:
            self.superior.choices = [(r.id, r.column_name) for r in Column_model.query.all()]  # 初始化所属栏目

    title = StringField(
        label='标题', validators=[validators.DataRequired(message='标题不可为空'), validators.Length(max=100, message='标题太长')],
        render_kw={"lay-verify": "required", "placeholder": "请输入标题", "style": "width: 75%;", "autocomplete": "off",
                   "class": "layui-input", "required": ""})
    superior = SelectField(label='所属栏目', coerce=int, validators=[validators.DataRequired('请选择栏目!')])
    thumb_img = StringField(
        label='缩略图',
        render_kw={"autocomplete": "off", "class": "layui-input", "placeholder": "http://",
                   "style": "display: inline-block;width: 75%;"})
    keyword = StringField(
        label='关键词',
        render_kw={"placeholder": "请输入关键词", "autocomplete": "off", "class": "layui-input", "style": "width:700px;"})
    description = TextAreaField(
        label='描述', render_kw={"class": "layui-textarea", "style": "width:700px;min-height:38px"})
    content = TextAreaField(
        label='内容',
        render_kw={"placeholder": "请输入内容", "class": "layui-textarea", "rows": "20", "cols": "80"})
    download1 = StringField(
        label='下载地址1', render_kw={"placeholder": "请输入下载地址1", "class": "layui-input", "autocomplete": "off",
                                  "style": "width:700px;"})
    download2 = StringField(
        label='下载地址2', render_kw={"placeholder": "请输入下载地址2", "class": "layui-input", "autocomplete": "off",
                                  "style": "width:700px;"})
    author = StringField(
        label='作者', default="佚名", validators=[validators.Length(max=64, message='作者过长!')],
        render_kw={"autocomplete": "off", "class": "layui-input", })
    source = StringField(
        label='来源', render_kw={"autocomplete": "off", "class": "layui-input", "placeholder": "TL_CMS"})
    promulgator = StringField(
        label='发布者', default="佚名", render_kw={"autocomplete": "off", "class": "layui-input"})
    contributor = StringField(
        label='投稿者', render_kw={"autocomplete": "off", "class": "layui-input"})
    permissions = SelectField(
        label='权限', choices=[(0, "开放"), (1, "一级会员"), (2, "二级会员")], render_kw={"lay-verify": "required"}, coerce=int)
    weight = StringField(
        label='权重', default=random_click, validators=[validators.DataRequired('权重值不可为空!'),validators.Regexp(r'^\d*$',message='权重格式为数字!') ],
        render_kw={"class": "layui-input", "style": "width: 108px;float: left;"})
    clicks = StringField(label='点击数', default=random_click, validators=[validators.DataRequired('点击数不可为空!'),validators.Regexp(r'^\d*$',message='点击数格式为数字!')],
                          render_kw={"class": "layui-input", "style": "width: 108px;float: left;"})
    update_time = DateField(label='时间', validators=[validators.DataRequired('时间不可为空!')],
                            render_kw={"class": "layui-input"})
    is_focus = BooleanField(
        label='', render_kw={"title": "焦点图", "lay-skin": "primary", "style": "width: 50px;"})
    is_roll = BooleanField(
        label='', render_kw={"title": "滚动图", "lay-skin": "primary", "style": "width: 50px;"})
    is_roll_text = BooleanField(
        label='', render_kw={"title": "滚动文字", "lay-skin": "primary", "style": "width: 50px;"})
    is_index_stick = BooleanField(
        label='', render_kw={"title": "首页置顶", "lay-skin": "primary", "style": "width: 50px;"})
    is_list_stick = BooleanField(
        label='', render_kw={"title": "列表置顶", "lay-skin": "primary", "style": "width: 50px;"})
    is_recommend = BooleanField(
        label='', render_kw={"title": "推荐", "lay-skin": "primary", "style": "width: 50px;"})
    is_pass = BooleanField(
        label='', render_kw={"title": "是否通过", "lay-skin": "primary", "checked": "", "style": "width: 50px;"})
